package com.test.image.controller;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.test.image.dto.ObjDTO;
import com.test.image.dto.TimeDTO;
import com.test.image.util.ListUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import sun.rmi.log.LogInputStream;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: image
 * @description:
 * @author: lishuang
 * @create: 2021-10-29 15:59
 **/
@RestController
public class ImageController {


    @GetMapping("/images")
    public Map<String, Object> getImageList() {
        String baseUrl = "https://images-1304904012.cos.ap-nanjing.myqcloud.com/";
        List<Integer> smallList = new ArrayList<>();
        List<Integer> bigList = new ArrayList<>();
        for (int i = 1; i <= 8; i++) {
            smallList.add(i);
        }
        for (int i = 9; i <= 16; i++) {
            bigList.add(i);
        }
        List<Integer> subList = ListUtil.getSubList(3, smallList);
        List<Integer> subList1 = ListUtil.getSubList(3, bigList);
        List<String> imagesList = new ArrayList<>();
        List<Integer> indexList = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            imagesList.add(baseUrl + subList.get(i) + ".png");
            indexList.add(subList.get(i));
            imagesList.add(baseUrl + subList1.get(i) + ".png");
            indexList.add(subList1.get(i));
        }
        Map<String, Object> map = new HashMap<>();
        map.put("indexList", indexList);
        map.put("images", imagesList);
        return map;
    }

    @PostMapping("/calculate")
    public Map<String, Object> calculate(@RequestBody Map<String, List<Object>> map) {
        ObjectMapper mapper = new ObjectMapper();
        List<ObjDTO>  answerList =  mapper.convertValue(map.get("answer"), new TypeReference<List<ObjDTO>>(){});
        List<TimeDTO>  timeDTOS =  mapper.convertValue(map.get("timeList"), new TypeReference<List<TimeDTO>>(){});

        Map<String, Object> calculate = ListUtil.calculate(answerList, timeDTOS);

        return calculate;
    }
}
